﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZP.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZP.UI
{
    public partial class ZP18 : EFViewCmes
    {
        public ZP18()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            if (initData.GetBlock(EIConstants.ResultBlock).GetRowCount() <= 0)
            {
                this.area.LoadView<EFCustomViewContainerPanel>("ZP19", new Dictionary<string, string>());
            }
        }

        /// <summary>
        /// 添加资源组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnAddButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.Grid.CheckedEIRows.Count == 1)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set(EIConstants.ServiceNameKey, "ZP15");
                eiInfo.Set(EIConstants.MethodNameKey, "queryRes");
                eiInfo.Set("roleId", this.Grid.CheckedEIRows[0]["postLabel"]);
                eiInfo.Set("methodLabel", "add");
                eiInfo.Set("title", "添加资源组");
                this.Submit(eiInfo, QueryResCompleted);
            }
            else
            {
                this.OpenEFWindowAlert("请选择一条记录进行操作！", true);
                return;
            }
        }

        /// <summary>
        /// 删除资源组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (this.Grid.CheckedEIRows.Count == 1)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set(EIConstants.ServiceNameKey, "ZP15");
                eiInfo.Set(EIConstants.MethodNameKey, "queryDelRes");
                eiInfo.Set("roleId", this.Grid.CheckedEIRows[0]["postLabel"]);
                eiInfo.Set("methodLabel", "delete");
                eiInfo.Set("title", "删除资源组");
                this.Submit(eiInfo, QueryResCompleted);
            }
            else
            {
                this.OpenEFWindowAlert("请选择一条记录进行操作！", true);
                return;
            }
        }

        /// <summary>
        /// 查询资源组的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryResCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                AddResourcesGroup view = new AddResourcesGroup();
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(e.EIInfo);

                view.roleId.Value = this.Grid.CheckedEIRows[0]["postLabel"];
                view.Method = e.EIInfo.Get("methodLabel").ToString();

                if ("delete".Equals(e.EIInfo.Get("methodLabel").ToString()))
                {
                    view.queryArea.Visibility = Visibility.Collapsed;
                    view.title = "删除";
                }

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = e.EIInfo.Get("title").ToString();//设置Title
                _popup.Content = view;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            }
            else
            {
                BaseTools.OpenEFWindowAlert("查询资源组失败:" + e.EIInfo.Msg, null);
                TempButton.IsEnabled = true;
            }

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                BaseTools.OpenEFWindowAlert("操作成功！", null);
                if (this.Grid.CheckedEIRows[0].Equals((this.Grid.SelectedItem as UIBaseEntity).GetEIRow()))
                {
                    LoadView();
                }
            }

            //按钮生效
            base.TempButtonOpen(true);
        }

        /// <summary>
        /// 点击所选择的列表发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            LoadView();
        }

        /// <summary>
        /// 加载ZP19资源组页面
        /// </summary>
        private void LoadView()
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            if (null != this.Grid.SelectedItem)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();            
                args.Add("roleId", _row["postLabel"].ToString());
            }
            this.area.LoadView<EFCustomViewContainerPanel>("ZP19", args);//加载从表页面
        }
    }
}
